#include <a_samp>

// === Defines === //

#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

#define COLOR_GREEN 0x33AA33AA
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_BRIGHTRED 0xFF0000AA

// === Functions === //

public OnFilterScriptInit()
{
	print("\n======================================================");
	print(" [FS] OnPlayerRequestClass creator by Matthias loaded.");
	print("======================================================\n");
	return 1;
}

public OnFilterScriptExit()
{
	print("\n========================================================");
	print(" [FS] OnPlayerRequestClass creator by Matthias unloaded.");
	print("========================================================\n");
	return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	dcmd(saveclass, 9, cmdtext);
	dcmd(viewclass, 9, cmdtext);
	return 0;
}

// === Commands === //

dcmd_saveclass(playerid, params[])
{
	if(!strlen(params)) return SendClientMessage(playerid, COLOR_WHITE, "Usage: /saveclass [filename]");

	new Float:X, Float:Y, Float:Z, filestr[64], string[128], Float:Ang;
	GetPlayerPos(playerid, X, Y, Z);
	GetPlayerFacingAngle(playerid, Ang);
	format(filestr, sizeof(filestr), "%s.txt", params);
	if(fexist(filestr)) return SendClientMessage(playerid, COLOR_BRIGHTRED, "There's already a file with that name! Please choose another filename.");

	new File:tempFile = fopen(filestr, io_append);
	fwrite(tempFile, "// Made using Matthias` OnPlayerRequestClass creator.\r\n");
	fwrite(tempFile, "// Put this outside other public functions.\r\n");
	fwrite(tempFile, "\r\n");
	fwrite(tempFile, "public OnPlayerRequestClass(playerid, classid)\r\n");
	fwrite(tempFile, "{\r\n");
	format(string, sizeof string, "	SetPlayerPos(playerid, %.4f, %.4f, %.4f);\r\n", X, Y, Z);
	fwrite(tempFile, string);
	format(string, sizeof string, "	SetPlayerInterior(playerid, %d);\r\n", GetPlayerInterior(playerid));
	fwrite(tempFile, string);
	format(string, sizeof string, "	SetPlayerFacingAngle(playerid, %.4f);\r\n", Ang);
	fwrite(tempFile, string);
	format(string, sizeof string, "	SetPlayerCameraPos(playerid, %.4f, %.4f, %.4f);\r\n", X - 3.5, Y + 4, Z + 1.5);
	fwrite(tempFile, string);
	format(string, sizeof string, "	SetPlayerCameraLookAt(playerid, %.4f, %.4f, %.4f);\r\n", X, Y, Z);
	fwrite(tempFile, string);
	fwrite(tempFile, "	return 1;\r\n");
	fwrite(tempFile, "}\r\n");
	fclose(tempFile);
	SendClientMessage(playerid, COLOR_GREEN, "Succesfully saved the class selection.");
    return 1;
}

dcmd_viewclass(playerid, params[])
{
	if(!strlen(params)) return SendClientMessage(playerid, COLOR_WHITE, "Usage: /viewclass [on/off]");

	new Float:X, Float:Y, Float:Z, Float:Ang;
	GetPlayerPos(playerid, X, Y, Z);
	GetPlayerFacingAngle(playerid, Ang);

	if(strcmp(params, "on", true) == 0)
	{
		SetPlayerCameraPos(playerid, X - 3.5, Y + 4, Z + 1.5);
		SetPlayerCameraLookAt(playerid, X, Y, Z);
		SendClientMessage(playerid, COLOR_GREEN, "Viewing the class camera.");
		TogglePlayerControllable(playerid, 0);
	}
	else if(strcmp(params, "off", true) == 0)
	{
		SetCameraBehindPlayer(playerid);
		SendClientMessage(playerid, COLOR_GREEN, "Stopped viewing the class camera.");
		TogglePlayerControllable(playerid, 1);
	}
	else return SendClientMessage(playerid, COLOR_WHITE, "Usage: /viewclass [on/off]");
	return 1;
}

